﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_KhuyenMaiVang.DAO;
using Project_KhuyenMaiVang.Models;
using System.Collections;
using System.Text.RegularExpressions;

namespace Project_KhuyenMaiVang.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {                      
            //add role
            AccountDAO accountDAO = new AccountDAO();
            AccountDTO accountInfo = accountDAO.getAccount(User.Identity.Name);
            if (accountInfo != null)
                ViewBag.Role = accountInfo.MaNhomNguoiDung;
            else
                ViewBag.Role = null;
            ViewBag.ID = Session["ID"];
            TinKhuyenMai_SanPham_HinhAnhDAO khuyenMaiSanPhamHinhAnhDao=new TinKhuyenMai_SanPham_HinhAnhDAO();
            List<TinKhuyenMai_SanPham_HinhAnhDTO> listkhuyenmai= khuyenMaiSanPhamHinhAnhDao.GetListNewestTKM_SP_HA((int)Session["MaKhuVuc"]);

            int n = listkhuyenmai.Count;
            for (int i = 0; i < n;i++ )
            {
                listkhuyenmai.ElementAt(i).ThongTinTomTat = CutFromString(listkhuyenmai.ElementAt(i).ThongTinTomTat, 105);
            }
            ViewBag.DsTinKhuyenMai = listkhuyenmai;
            

            MemberDAO mb = new MemberDAO();
            MemberDTO a = mb.GetInfor(3);   
          

            KhuVucDAO khuVucDao=new KhuVucDAO();
            List<KhuVucDTO> DsKhuVuc= khuVucDao.GetListAreas();
            ViewBag.DsKhuVuc = DsKhuVuc;
            return View();            
        }

        [HttpPost]
        public ActionResult Index(IndexModel indexModel, string ThemeReplace, FormCollection formCollection)
        {
            string value = ThemeReplace;
            if(value!=null)
            {
                Session["Theme"] = Int32.Parse(value);
            }

            if(indexModel.MaThanhPho!=0)
            {
                Session["MaKhuVuc"] = indexModel.MaThanhPho;    
            }            
            AccountDAO accountDAO = new AccountDAO();
            AccountDTO accountInfo = accountDAO.getAccount(User.Identity.Name);
            if (accountInfo != null)
                ViewBag.Role = accountInfo.MaNhomNguoiDung;
            else
                ViewBag.Role = null;
            ViewBag.ID = Session["ID"];
            TinKhuyenMai_SanPham_HinhAnhDAO khuyenMaiSanPhamHinhAnhDao = new TinKhuyenMai_SanPham_HinhAnhDAO();
            List<TinKhuyenMai_SanPham_HinhAnhDTO> listkhuyenmai = khuyenMaiSanPhamHinhAnhDao.GetListNewestTKM_SP_HA((int)Session["MaKhuVuc"]);

            int n = listkhuyenmai.Count;
            for (int i = 0; i < n; i++)
            {
                listkhuyenmai.ElementAt(i).ThongTinTomTat = CutFromString(listkhuyenmai.ElementAt(i).ThongTinTomTat, 105);
            }

            ViewBag.DsTinKhuyenMai = listkhuyenmai;
            KhuVucDAO khuVucDao = new KhuVucDAO();
            List<KhuVucDTO> DsKhuVuc = khuVucDao.GetListAreas();
            ViewBag.DsKhuVuc = DsKhuVuc;
            //Thực hiên insert email vào database.
            
            if(indexModel.EmailDangKy !=null)
            {
                //Kiem tra tinh hop le cua email
                if(KiemTraDinhDangEmail(indexModel.EmailDangKy))
                {
                    DangKyNhanTinKhuyenMaiDAO dangKyNhanTinKhuyenMaiDao = new DangKyNhanTinKhuyenMaiDAO();
                    dangKyNhanTinKhuyenMaiDao.insertEmail(indexModel.EmailDangKy);
                    ViewBag.FormToggle = "dang ky thanh cong";   
                }
                else
                {
                    ViewBag.FormToggle = "Chưa đúng định dạng email";
                }
            }
           
            return View();
        }


        public bool KiemTraDinhDangEmail(string email)
        {
            Regex regex=new Regex(@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$");
            if(regex.IsMatch(email))
            {
                return true;
            }
            return false;
        }

        public ActionResult XemChiTietSanPham(int idSanPham)
        {            
            AccountDAO accountDAO = new AccountDAO();
            AccountDTO accountInfo = accountDAO.getAccount(User.Identity.Name);
            if (accountInfo != null)
                ViewBag.Role = accountInfo.MaNhomNguoiDung;
            else
                ViewBag.Role = null;
            ViewBag.ID = Session["ID"];
            
            //Tìm tin khuyến mãi theo ID.
            TinKhuyenMai_SanPham_HinhAnhDAO khuyenMaiSanPhamHinhAnhDao = new TinKhuyenMai_SanPham_HinhAnhDAO();
            TinKhuyenMai_SanPham_HinhAnhDTO ChiTietTinKhuyenMai =
                khuyenMaiSanPhamHinhAnhDao.ThongTinChiTietTinKhuyenMai(idSanPham);
            ViewBag.ThongTinChiTietTinKhuyenMai = ChiTietTinKhuyenMai;

            //Lay 5 tin khuyen mai hot
            TinKhuyenMai_SanPham_HinhAnhDAO tinKhuyenMaiSanPhamHinhAnhDao=new TinKhuyenMai_SanPham_HinhAnhDAO();
            List<TinKhuyenMai_SanPham_HinhAnhDTO> DsTinKhuyenMaiHot =
                tinKhuyenMaiSanPhamHinhAnhDao.GetListHottestTKM_SP_HA(5);
            ViewBag.DSKhuyenMaiHot = DsTinKhuyenMaiHot;


            //Lay List khu vuc
            KhuVucDAO khuVucDao = new KhuVucDAO();
            List<KhuVucDTO> DsKhuVuc = khuVucDao.GetListAreas();
            ViewBag.DsKhuVuc = DsKhuVuc;

            List<Comment> commentListToView = getCommentList(accountDAO, ChiTietTinKhuyenMai.MaTinKhuyenMai);
            ViewBag.CommentList = commentListToView;
            return View();
        }

        private static List<Comment> getCommentList(AccountDAO accountDAO, int maTinKhuyenMai)
        {
            //Comments
            CommentDAO commentDAO = new CommentDAO();
            List<CommentDTO> commentList = commentDAO.getListCommentsBy(maTinKhuyenMai);
            List<Comment> commentListToView = new List<Comment>();
            for (int i = 0; i < commentList.Count; i++)
            {
                AccountDTO acc = accountDAO.getAccount(commentList[i].IDAccount);
                Comment temp = new Comment();
                temp.NguoiPost = acc.UserName;
                temp.NoiDung = commentList[i].NoiDung;
                temp.NgayComment = commentList[i].NgayComment;
                commentListToView.Add(temp);
            }
            return commentListToView;
        }

        public ActionResult ThongTinGiaoHang(int MaTinKhuyenMai)
        {
            AccountDAO accountDAO = new AccountDAO();
            AccountDTO accountInfo = accountDAO.getAccount(User.Identity.Name);
            if (accountInfo != null)
                ViewBag.Role = accountInfo.MaNhomNguoiDung;
            else
                ViewBag.Role = null;
            ViewBag.ID = Session["ID"];

            //Tìm tin khuyến mãi theo ID.
            TinKhuyenMai_SanPham_HinhAnhDAO khuyenMaiSanPhamHinhAnhDao = new TinKhuyenMai_SanPham_HinhAnhDAO();
            TinKhuyenMai_SanPham_HinhAnhDTO ChiTietTinKhuyenMai =
                khuyenMaiSanPhamHinhAnhDao.ThongTinChiTietTinKhuyenMai(MaTinKhuyenMai);

            //Thong tin tin khuyen mai
            ViewBag.ThongTinChiTietTinKhuyenMai = ChiTietTinKhuyenMai;
            //Thong danh sach cach hinh thuc khuyen mai.
            HinhThucThanhToanDAO hinhThucThanhToanDao=new HinhThucThanhToanDAO();
            List<HinhThucThanhToanDTO> dsHinhThucThanhToan=hinhThucThanhToanDao.GetListHinhThucThanhToan();
            ViewBag.ListHinhThucThanhToan = dsHinhThucThanhToan;
            
            IndexModel model=new IndexModel();            
            model.ThongTinGiaoHangModel.ThongTinDonDatHang.SoLuong = 1;
            model.MaThanhPho = (int) Session["MaKhuVuc"];

            //Lay 5 tin khuyen mai hot
            TinKhuyenMai_SanPham_HinhAnhDAO tinKhuyenMaiSanPhamHinhAnhDao = new TinKhuyenMai_SanPham_HinhAnhDAO();
            List<TinKhuyenMai_SanPham_HinhAnhDTO> DsTinKhuyenMaiHot =
                tinKhuyenMaiSanPhamHinhAnhDao.GetListHottestTKM_SP_HA(5);
            ViewBag.DSKhuyenMaiHot = DsTinKhuyenMaiHot;

            //Khu vuc nhan hang
            KhuVucDAO khuVucDao = new KhuVucDAO();
            List<KhuVucDTO> DsKhuVuc = khuVucDao.GetListAreas();
            ViewBag.DsKhuVuc = DsKhuVuc;

            return View(model);            
        }

        [HttpPost]
        public ActionResult ThongTinGiaoHang(IndexModel indexModel)
        {
            //Lay 5 tin khuyen mai hot
            TinKhuyenMai_SanPham_HinhAnhDAO tinKhuyenMaiSanPhamHinhAnhDao = new TinKhuyenMai_SanPham_HinhAnhDAO();
            List<TinKhuyenMai_SanPham_HinhAnhDTO> DsTinKhuyenMaiHot =
                tinKhuyenMaiSanPhamHinhAnhDao.GetListHottestTKM_SP_HA(5);
            ViewBag.DSKhuyenMaiHot = DsTinKhuyenMaiHot;

            //Luu lai thong tin 
            KhuVucDAO khuVucDao = new KhuVucDAO();
            List<KhuVucDTO> DsKhuVuc = khuVucDao.GetListAreas();
            ViewBag.DsKhuVuc = DsKhuVuc;

            return View("HinhThucThanhToan", indexModel);
            
        }


        public ActionResult HinhThucThanhToan(IndexModel indexModel)
        {
            indexModel.MaThanhPho = (int) Session["MaKhuVuc"];
            return View();
        }

        public PartialViewResult AddComment(String CommentContent, String MaTinKM, String NgayDang)
        {
            CommentDTO comment = new CommentDTO();
            AccountDAO accDAO = new AccountDAO();
            AccountDTO accDTO = accDAO.getAccount(User.Identity.Name);
            CommentDAO commentDAO = new CommentDAO();
            comment.MaTinKhuyenMai = Int32.Parse(MaTinKM);
            comment.IDAccount = accDTO.IDAccount;
            comment.NoiDung = CommentContent;
            comment.NgayComment = DateTime.Parse(NgayDang);
            commentDAO.insertComment(comment);
            comment.NoiDung = CommentContent;
            List<Comment> commentList = getCommentList(accDAO, Int32.Parse(MaTinKM));
            return PartialView("_CommentView", commentList);
        }


        public string CutFromString(string input, int slg)
        {
            string result = "";
            int num = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == ' ')
                {
                    if (i <= slg)
                        num++;
                    else
                        break;
                }
            }

            string[] temp = input.Split(' ');
            for (int i = 0; i < num; i++)
            {
                result += (temp[i] + " ");
            }
            result += "...";
            return result;
        }
    }
}
